package com.example.demo.Service;

import com.example.demo.entity.UserFollow;
import com.example.demo.repository.UserFollowRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserFollowService {

    @Autowired
    private UserFollowRepository userFollowRepository;

    public void followUser(Long followerId, Long followeeId) {
        if (!userFollowRepository.existsByFollowerIdAndFolloweeId(followerId, followeeId)) {
            UserFollow userFollow = new UserFollow();
            userFollow.setFollowerId(followerId);
            userFollow.setFolloweeId(followeeId);
            userFollowRepository.save(userFollow);
        }
    }

    // UserFollowService.java
    public List<UserFollow> getFollowersList(Long followerId) {
        return userFollowRepository.findFolloweeIdsByFollowerId(followerId).stream()
                .map(followeeId -> {
                    UserFollow userFollow = new UserFollow();
                    userFollow.setFollowerId(followerId);
                    userFollow.setFolloweeId(followeeId);
                    return userFollow;
                })
                .collect(Collectors.toList());
    }

    public void unfollowUser(Long followerId, Long followeeId) {
        userFollowRepository.deleteByFollowerIdAndFolloweeId(followerId, followeeId);
    }

    public boolean isFollowed(Long followerId, Long followeeId) {
        return userFollowRepository.existsByFollowerIdAndFolloweeId(followerId, followeeId);
    }
}